﻿using ContentAppBase.Model;
using ContentAppBase.Model.Bing;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ContentAppBase.ViewModel
{
    public class MainViewModel : AsyncViewModelBase
    {
        public RelayCommand InitCommand { get; private set; }
        public RelayCommand GetImageResultsCommand { get; private set; }
        public RelayCommand GetWebResultsCommand { get; private set; }

        private ObservableCollection<GroupInfoList<object>> _sections;
        public ObservableCollection<GroupInfoList<object>> Sections
        {
            get { return _sections; }
            set { _sections = value; RaisePropertyChanged("Sections"); }
        }

        private bool _isZoomedIn = true;
        public bool IsZoomedIn
        {
            get { return _isZoomedIn; }
            set { _isZoomedIn = value; RaisePropertyChanged("IsZoomedIn"); }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; RaisePropertyChanged("Title"); }
        }

        private string _legalNotice;
        public string LegalNotice
        {
            get { return _legalNotice; }
            set { _legalNotice = value; RaisePropertyChanged("LegalNotice"); }
        }

        private string _imageResultTitle = "Bilder";
        public string ImageResultTitle
        {
            get { return _imageResultTitle; }
            set { _imageResultTitle = value; RaisePropertyChanged("ImageResultTitle"); }
        }

        private string _webResultTitle = "Webseiten";
        public string WebResultTitle
        {
            get { return _webResultTitle; }
            set { _webResultTitle = value; RaisePropertyChanged("WebResultTitle"); }
        }

        private Uri _shareUrl;
        public Uri ShareUrl
        {
            get { return _shareUrl; }
            set { _shareUrl = value; RaisePropertyChanged("ShareUrl"); }
        }

        private List<ResultBase> _imageResults;
        public List<ResultBase> ImageResults
        {
            get { return _imageResults; }
            set { _imageResults = value; RaisePropertyChanged("ImageResults"); }
        }

        private List<ResultBase> _webResults;
        public List<ResultBase> WebResults
        {
            get { return _webResults; }
            set { _webResults = value; RaisePropertyChanged("WebResults"); }
        }

        private List<ResultBase> _currentResults;
        public List<ResultBase> CurrentResults
        {
            get { return _currentResults; }
            set { _currentResults = value; RaisePropertyChanged("CurrentResults"); }
        }

        public MainViewModel()
        {
            InitCommand = new RelayCommand(Init);
            GetImageResultsCommand = new RelayCommand(GetImageResults);
            GetWebResultsCommand = new RelayCommand(GetWebResults);
        }

        private async void Init()
        {
            if (_sections == null)
            {
                IsLoading = true;
                Sections = new ObservableCollection<GroupInfoList<object>>();

                var dataset = await DataService.Current.GetDataset();
                Title = dataset.Title;
                LegalNotice = dataset.LegalNotice;
                ShareUrl = new Uri(dataset.ShareUrl, UriKind.Absolute);

                if (dataset.HeroImage != null)
                    dataset.HeroImage.Url = String.Format("ms-appx:///Assets/{0}", dataset.HeroImage.Url);

                Sections.Add(new GroupInfoList<object>("Bild", new SimpleItem() { Content = dataset.HeroImage, Type = SimpleItemType.HeroImage }, true, false));
                Sections.Add(new GroupInfoList<object>("Einführung", new SimpleItem() { Content = dataset.Intro, Type = SimpleItemType.Intro }, false, false));

                var imageResults = await DataService.Current.SearchBingAsync(dataset.Title, Model.Bing.SearchTypes.Image, 16);
                if (imageResults != null && imageResults.Count > 0)
                    Sections.Add(new GroupInfoList<object>(_imageResultTitle, new SimpleItem() { Content = imageResults, Type = SimpleItemType.ImageResults }));

                var webResults = await DataService.Current.SearchBingAsync(dataset.Title, Model.Bing.SearchTypes.Web, 8);
                if (webResults != null && webResults.Count > 0)
                    Sections.Add(new GroupInfoList<object>(_webResultTitle, new SimpleItem() { Content = webResults, Type = SimpleItemType.WebResults }));

                foreach (var chapter in dataset.Chapters)
                {
                    Sections.Add(new GroupInfoList<object>(chapter.Title, new SimpleItem() { Content = chapter.RawContent, Type = SimpleItemType.Chapter }));
                }

                IsLoading = false;
            }
        }

        private async void GetImageResults()
        {
            CurrentResults = null;
            if (_imageResults == null)
            {
                IsLoading = true;
                ImageResults = await DataService.Current.SearchBingAsync(_title, SearchTypes.Image);
                IsLoading = false;
            }

            CurrentResults = ImageResults;
        }

        private async void GetWebResults()
        {
            CurrentResults = null;
            if (_webResults == null)
            {
                IsLoading = true;
                WebResults = await DataService.Current.SearchBingAsync(_title, SearchTypes.Web);
                IsLoading = false;
            }

            CurrentResults = WebResults;
        }
    }
}
